<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div style="width:350px; margin:0px auto; margin-top:150px;">
	<form  class="layui-form" action="">
		<c:forEach items="${show.forms}" var="field">
			<c:if test="${field.type=='hidden'}">
				<input type="hidden" name ="${field.field}"  id="${field.field}"/>
			</c:if>
			<c:if test="${field.type=='input'}">
				<c:if test="${field.flag == 11||field.flag == 10}">
					<div class="layui-form-item">
					 <label class="layui-form-label">${field.name}</label>
					  <div class="layui-inline">
					    <input style="width:190px;" class="layui-input" name="${field.field}" id="${field.field}" autocomplete="off">
					  </div>
					</div>
				</c:if>
			</c:if>
			<c:if test="${field.type=='select'}">
				<c:if test="${field.flag == 11||field.flag == 1}">
					<div class="layui-form-item">
				<div class="layui-inline">
			      <label class="layui-form-label">${field.name}</label>
			      <div class="layui-input-inline">
			        <select id="${field.field}"  name="${field.field}" lay-verify="required" lay-search="">
			        	<c:forEach items="${field.options}" var="option">
			        		<c:if test="${option.type==0}">
								<c:if test="${option.value != ''}">
									<option value="${option.value}">${option.name}</option>
								</c:if>
			        		</c:if>
			        	</c:forEach>
			        </select>
			      </div>
			    </div>
			    </div>
				</c:if>
			</c:if>
		</c:forEach>
		 <button style="width:240px; margin-left:60px;" type="button" class="layui-btn com">确认</button>
	</form>
</div>
</body>
	<script type="text/javascript">
	function loadData(){
		if(${flag==1}){
			$(${show.forms}).each(function(i,n){
				var obj = eval(${obj});
				var val = obj[n.field];
				if(n.type=='input'||n.type=='hidden'){
					$('#'+n.field).val(''+val);
				}else if(n.type=='select'){
					$('#'+n.field).val(val);
				}
					
			})
		}
	}
	
	
	layui.use(['table','form'], function(){
		 var table = layui.table
		 ,form = layui.form;
		
		 <c:forEach items="${show.forms}" var="temp">
		 	<c:if test="${temp.options[0].type == 1}">
			 		$.ajax({
					   type: "get",
					   url: "/pro${temp.options[0].name}",
					   async: false,
					   success: function(data){
						   var $temp = $("#${temp.field}");
						   $temp.html("");
						   $(data).each(function(i,n){
							   $temp.append("<option value="+n.id+">"+n.name+"</option>");
						   })
					   }
					});
		 	</c:if>
	 	</c:forEach>
		loadData();	 	
		form.render();
	})
	</script>
	<script type="text/javascript">
		$(function(){
			$(".com").click(function(){
				var url ="";
				if(${flag ==0}){
					url = "${show.addurl}";
				}else {
					url = "${show.updurl}";
				}
				//发送ajax请求修改
				$.ajax({
		  			   type: "get",
		  			   url: "/pro/"+url,
		  			   async: false,
		  			   data:$(".layui-form").serialize(),
		  			   success:function(data){
		  				 if(data){
		  					$('.layui-body').load('/pro/toManager?id=${id}');
		  				 }else{
		  					 layer.open({
			  					  type: 1, 
			  					  content: '<div style="padding: 20px 100px;">'+ '操作失败' +'</div>' //这里content是一个普通的String
		  					 });
		  				  }
		  			   }
				})
				
			})
		})
	</script>
</html>